

#include "graphics.h"
#include "macros.h"
#include "f32.h"
#include "gameApp.h"
#import <UIKit/UIKit.h>
#import <OpenGLES/ES1/gl.h>

void Graphics::DrawRect( _Rect& r, RGBVAL color )
{	
	float red = RGB_GET_RED(color) / 255.0f;
	float green = RGB_GET_GREEN(color) / 255.0f;
	float blue = RGB_GET_BLUE(color) / 255.0f;
	
	GLfloat vertices[] = {
		r.x,			r.y,			1,
		r.x + r.dx,		r.y,			1,
		r.x + r.dx,		r.y + r.dy,		1,
		r.x,			r.y + r.dy,		1
	};
	
	glDisable( GL_BLEND );
	glDisable( GL_TEXTURE_2D );
	
	glColor4f( red, green, blue, 1 );
	glVertexPointer(3, GL_FLOAT, 0, vertices);
	glDrawArrays(GL_LINE_LOOP, 0, 4);
	
	glEnable(GL_TEXTURE_2D);
	glEnable( GL_BLEND );
}

void Graphics::FillRect( _Rect& r, RGBVAL color )
{
	float red = RGB_GET_RED(color) / 255.0f;
	float green = RGB_GET_GREEN(color) / 255.0f;
	float blue = RGB_GET_BLUE(color) / 255.0f;
	
	GLfloat vertices[] = {
		r.x,			r.y,			1,
		r.x + r.dx,		r.y,			1,
		r.x,			r.y + r.dy,		1,
		r.x + r.dx,		r.y + r.dy,		1
	};
	
	glDisable( GL_BLEND );
	glDisable( GL_TEXTURE_2D );
	
	glColor4f( red, green, blue, 1 );
	glVertexPointer(3, GL_FLOAT, 0, vertices);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	
	glEnable(GL_TEXTURE_2D);
	glEnable( GL_BLEND );
}

void Graphics::DrawRect( int16 x,int16 y,int16 width,int16 height, RGBVAL color )
{
	_Rect r(x,y,width,height);
	DrawRect(r,color);
}

void Graphics::FillRect( int16 x,int16 y,int16 width,int16 height, RGBVAL color )
{
	_Rect r(x,y,width,height);
	FillRect(r,color);
}

#define PI2 6.28
void Graphics::DrawCircle( float centerX,float centerY, float radius, RGBVAL color)
{
	float red = RGB_GET_RED(color) / 255.0f;
	float green = RGB_GET_GREEN(color) / 255.0f;
	float blue = RGB_GET_BLUE(color) / 255.0f;
	
	GLfloat vertices[1080] = { 0, };	
	glDisable( GL_BLEND );
	glDisable( GL_TEXTURE_2D );
	
	glColor4f( red, green, blue, 1 );
	for (int i = 0;i < 360; i++)
	{
		vertices[i * 3] = centerX + radius * cos(PI2 * i / 360);
		vertices[i * 3 + 1] = centerY + radius * sin(PI2 * i / 360);
		vertices[i * 3 + 2] = 1;
	}
	glVertexPointer(3, GL_FLOAT, 0, vertices);
	glDrawArrays(GL_POINTS, 0, 360);

	glEnable(GL_TEXTURE_2D);
	glEnable( GL_BLEND );
}

void Graphics::FillCircle( float centerX,float centerY, float radius, RGBVAL color)
{
	float red = RGB_GET_RED(color) / 255.0f;
	float green = RGB_GET_GREEN(color) / 255.0f;
	float blue = RGB_GET_BLUE(color) / 255.0f;
	
	GLfloat vertices[1080] = { 0, };	
	glDisable( GL_BLEND );
	glDisable( GL_TEXTURE_2D );
	
	glColor4f( red, green, blue, 1 );
	for (int i = 0;i < 360; i++)
	{
		vertices[i * 3] = centerX + radius * cos(PI2 * i / 360);
		vertices[i * 3 + 1] = centerY + radius * sin(PI2 * i / 360);
		vertices[i * 3 + 2] = 1;
	}
	glVertexPointer(3, GL_FLOAT, 0, vertices);
	glDrawArrays(GL_TRIANGLE_FAN, 0, 360);
	
	glEnable(GL_TEXTURE_2D);
	glEnable( GL_BLEND );
}

void Graphics::DrawScollBar(int32 x,int32 y,int32 width,int32 height,int32 posnow,int32 scrolllen)
{
	int touchState = CGameApp::GetTouchInput()->GetEvent();
	const float maxtime = 10,defaultBarH = 50,minBarH = 5;
	static float timenow = 0,dir = 0,delaytime = 0,lastpos = 0;
	
	if(touchState == TOEV_MOVE)
	{
		delaytime = 0;
		dir = 1;
		if(timenow == 0)timenow = 1;
	}
	else if(touchState == TOEV_UP)
	{
		delaytime = 20;
	}
	if(lastpos != posnow)lastpos = posnow;
	else if(delaytime > 0 && timenow == maxtime)delaytime--;
	if(delaytime == 1)
	{
		dir = -1;
		delaytime = 0;
	}

    if(dir > 0 && timenow < maxtime)timenow += dir;
	if(dir < 0 && timenow > 0)timenow += dir;
	
	if(timenow != 0)
	{
		float alpha = timenow*0.6/maxtime;
		
		float red = 0;//RGB_GET_RED(color) / 255.0f;
		float green = 0;//RGB_GET_GREEN(color) / 255.0f;
		float blue = 0;//RGB_GET_BLUE(color) / 255.0f;
		
		int32 barH = height - scrolllen; 
		if(barH < 0)barH = defaultBarH;
		
		int32 scollHMax = height-barH;
		
		int32 posx = x;
		int32 posy = y + posnow*scollHMax/scrolllen;
		 
		if(posnow < 0)
		{
			posy = y;
			barH += posnow;
			if(barH < minBarH)barH = minBarH;
		}
		if(posnow > scrolllen)
		{
			barH += scrolllen - posnow;
			if(barH < minBarH)barH = minBarH;
			posy = y+height-barH;
		}
		
		_Rect r(posx,posy,width,barH);
		
		GLfloat vertices[] = {
			r.x,			r.y,			1,
			r.x + r.dx,		r.y,			1,
			r.x,			r.y + r.dy,		1,
			r.x + r.dx,		r.y + r.dy,		1
		};
		GLfloat vertices_2[540] = { 0, };
		
		glDisable( GL_TEXTURE_2D );
		
		glColor4f( red, green, blue, alpha );
		glVertexPointer(3, GL_FLOAT, 0, vertices);
		glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
		
		float radius = r.dx>>1;
		for (int i = 0;i < 180; i++)
		{
			vertices_2[i * 3] = r.x + radius + radius * cos(PI2 * (i+180) / 360);
			vertices_2[i * 3 + 1] = r.y + radius * sin(PI2 * (i+180) / 360);
			vertices_2[i * 3 + 2] = 1;
		}
		glVertexPointer(3, GL_FLOAT, 0, vertices_2);
		glDrawArrays(GL_TRIANGLE_FAN, 0, 180);
		
		for (int i = 0;i < 180; i++)
		{
			vertices_2[i * 3] = r.x + radius + radius * cos(PI2 * i / 360);
			vertices_2[i * 3 + 1] = r.y + barH + radius * sin(PI2 * i / 360);
			vertices_2[i * 3 + 2] = 1;
		}
		glVertexPointer(3, GL_FLOAT, 0, vertices_2);
		glDrawArrays(GL_TRIANGLE_FAN, 0, 180);
		
		glColor4f( 1, 1, 1, 1 );
		glEnable(GL_TEXTURE_2D);
	}
}

